package com.hjq.demo.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.hjq.demo.R;
import com.hjq.demo.app.AppAdapter;
import com.hjq.demo.http.api.UserListApi;
import com.hjq.demo.other.NewConfig;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.appcompat.widget.AppCompatTextView;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/09/22
 *    desc   : 客户信息记录列表
 */
public final class CustomerInformationRecordAdapter extends AppAdapter<UserListApi.Bean.RowsBean> {
    public  Boolean isSelect = false;
    public  Boolean isAllSelect = false;
    public OnListener onListener;

    public void setOnListener(CustomerInformationRecordAdapter.OnListener onListener){
        this.onListener = onListener;
    }

    public interface OnListener {
        void acbIsSelectListener();
    }
    public CustomerInformationRecordAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }




    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private AppCompatTextView tvCustomerName;
        private AppCompatTextView tvTime;
        private AppCompatTextView tvCustomerTelephone;
        private AppCompatTextView tvCustomerAddress;
        private AppCompatCheckBox acbIsSelect;
        private LinearLayout llIsSelect;

        private ViewHolder() {
            super(R.layout.customer_information_ecord_item);
            tvCustomerName = (AppCompatTextView) findViewById(R.id.tv_customer_name);
            tvTime = (AppCompatTextView) findViewById(R.id.tv_time);
            tvCustomerTelephone = (AppCompatTextView) findViewById(R.id.tv_customer_telephone);
            tvCustomerAddress = (AppCompatTextView) findViewById(R.id.tv_customer_address);
            llIsSelect = (LinearLayout) findViewById(R.id.ll_is_select);
            acbIsSelect = (AppCompatCheckBox) findViewById(R.id.acb_is_select);
        }

        @Override
        public void onBindView(int position) {

            tvCustomerName.setText(NewConfig.settingColorSpan("名称："+getItem(position).getUsername(),"名称："));
            tvTime.setText(NewConfig.settingColorSpan("创建时间："+getItem(position).getCreatedTime(),"创建时间："));
            tvCustomerTelephone.setText(NewConfig.settingColorSpan("手机号："+getItem(position).getPhoneNumber(),"手机号："));
            tvCustomerAddress.setText(NewConfig.settingColorSpan("地址："+getItem(position).getAddress(),"地址："));
            if (isSelect){
                llIsSelect.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getItem(position).setSelect(!getItem(position).getSelect());
                        onListener.acbIsSelectListener();
                        notifyItemChanged(position);
                    }
                });
            }
            acbIsSelect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getItem(position).setSelect(!getItem(position).getSelect());
                    onListener.acbIsSelectListener();
                    notifyItemChanged(position);
                }
            });
            if (getItem(position).getSelect() == null){
                getItem(position).setSelect(false);
            }
            acbIsSelect.setChecked(getItem(position).getSelect());
        }
    }
    public void CustomerRecordAdapterIsSelect(Boolean isSelect) {
        this.isSelect =  isSelect;
        notifyDataSetChanged();
    }
    public void CustomerRecordAdapterIsAllSelect(Boolean newChooseAll ) {
        this.isAllSelect =  !newChooseAll;
        //判断是否全选
        if (isAllSelect) {
            for (int i = 0; i < getData().size(); i++) {
                getItem(i).setSelect(true);
            }
        }  else{
            for (int i = 0; i < getData().size(); i++) {
                getItem(i).setSelect(false);
            }
        }
        notifyDataSetChanged();
    }
}